/*
 *  Author: Honrun
 */
#ifndef DEVICESSOFTTIMER_H_INCLUDED
#define DEVICESSOFTTIMER_H_INCLUDED


#include "stdint.h"
#include "DevicesList.h"





typedef enum {
    softTimerClose = 0x00,    /* 定时器已关闭 */
    softTimerOpen = 0x01,     /* 正常计时当中 */
    softTimerOver = 0x02,     /* 定时器已溢出 */
    softTimerError = 0x08,    /* 定时器错误 */
    softTimerCircular = 0x10, /* 循环计时 */
}SoftTimerStateEnum;


typedef struct
{
    int64_t timeStop;           /* 定时结束时刻（us） */
    int64_t timeDuration;       /* 定时时长（us） */
    SoftTimerStateEnum state;   /* 状态 */
}SoftTimerTypeDef;


typedef struct
{
    SoftTimerTypeDef timer;
    void (*vCallbackFunction)(void *);  /* 回调函数 */

    ListType typeListExe;               /* 执行链表 */
    ListType typeListDel;               /* 删除链表 */
}SoftTimerTaskTypeDef;






int64_t lSoftTimerGetNow(void);


/* 基础定时 功能 */
#define cSoftTimerSetUs(ptypeTimer, lTime, state) cSoftTimerSet((ptypeTimer), (lTime), (state))
#define cSoftTimerSetMs(ptypeTimer, lTime, state) cSoftTimerSet((ptypeTimer), (lTime) * 1000.0f, (state))
#define cSoftTimerSetS(ptypeTimer, lTime, state) cSoftTimerSet((ptypeTimer), (lTime) * 1000000.0f, (state))
int8_t cSoftTimerSet(SoftTimerTypeDef *ptypeTimer, int64_t lTime, SoftTimerStateEnum state);
int8_t cSoftTimerReset(SoftTimerTypeDef *ptypeTimer);
int8_t cSoftTimerReload(SoftTimerTypeDef *ptypeTimer);
int8_t cSoftTimerOpen(SoftTimerTypeDef *ptypeTimer);
int8_t cSoftTimerClose(SoftTimerTypeDef *ptypeTimer);
int8_t cSoftTimerSetState(SoftTimerTypeDef *ptypeTimer, SoftTimerStateEnum enumState);
SoftTimerStateEnum enumSoftTimerGetState(SoftTimerTypeDef *ptypeTimer);


/* 定时调用执行 功能 */
#define cSoftTimerCreateUs(ptypeTimer, lTime, state, vCallbackFunction) cSoftTimerCreate((ptypeTimer), (lTime), (state), (vCallbackFunction))
#define cSoftTimerCreateMs(ptypeTimer, lTime, state, vCallbackFunction) cSoftTimerCreate((ptypeTimer), (lTime) * 1000.0f, (state), (vCallbackFunction))
#define cSoftTimerCreateS(ptypeTimer, lTime, state, vCallbackFunction) cSoftTimerCreate((ptypeTimer), (lTime) * 1000000.0f, (state), (vCallbackFunction))
int8_t cSoftTimerCreate(SoftTimerTaskTypeDef *ptypeTimer, int64_t lTime, SoftTimerStateEnum state, void (*vCallbackFunction)(void *pvHandle));
int8_t cSoftTimerMachine(void);

#endif // DEVICESSOFTTIMER_H_INCLUDED
